/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace DaveSexton.DocProject.Sandcastle.ContentManagement
{
	[Serializable]
	public class ContentItemAttributeDataCollection : KeyedCollection<string, ContentItemAttributeData>, IBindingList, ICancelAddNew
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="ContentItemAttributeDataCollection" /> class.
		/// </summary>
		public ContentItemAttributeDataCollection()
		{
		}
		#endregion

		#region Methods
		protected override string GetKeyForItem(ContentItemAttributeData item)
		{
			return item.Name;
		}

		protected override void ClearItems()
		{
			base.ClearItems();

			OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
		}

		protected override void RemoveItem(int index)
		{
			base.RemoveItem(index);

			OnListChanged(new ListChangedEventArgs(ListChangedType.ItemDeleted, index));
		}

		protected override void InsertItem(int index, ContentItemAttributeData item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			base.InsertItem(index, item);

			OnListChanged(new ListChangedEventArgs(ListChangedType.ItemAdded, index));
		}

		protected override void SetItem(int index, ContentItemAttributeData item)
		{
			if (item == null)
				throw new ArgumentNullException("item");

			base.SetItem(index, item);

			OnListChanged(new ListChangedEventArgs(ListChangedType.ItemChanged, index));
		}
		#endregion

		#region Events
		private readonly object ListChangedEventLock = new object();
		private ListChangedEventHandler ListChangedEvent;

		/// <summary>
		/// Event raised when the list changes.
		/// </summary>
		public event ListChangedEventHandler ListChanged
		{
			add
			{
				lock (ListChangedEventLock)
				{
					ListChangedEvent += value;
				}
			}
			remove
			{
				lock (ListChangedEventLock)
				{
					ListChangedEvent -= value;
				}
			}
		}

		/// <summary>
		/// Raises the <see cref="ListChanged" /> event.
		/// </summary>
		/// <param name="e"><see cref="ListChangedEventArgs" /> object that provides the arguments for the event.</param>
		protected virtual void OnListChanged(ListChangedEventArgs e)
		{
			ListChangedEventHandler handler = null;

			lock (ListChangedEventLock)
			{
				handler = ListChangedEvent;
			}

			if (handler != null)
				handler(this, e);
		}
		#endregion

		#region IBindingList Members
		public void AddIndex(PropertyDescriptor property)
		{
			// do nothing
		}

		public virtual object AddNew()
		{
			throw new NotSupportedException();
		}

		public bool AllowEdit
		{
			get
			{
				return true;
			}
		}

		public virtual bool AllowNew
		{
			get
			{
				return false;
			}
		}

		public bool AllowRemove
		{
			get
			{
				return true;
			}
		}

		public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
		{
			throw new NotSupportedException();
		}

		public int Find(PropertyDescriptor property, object key)
		{
			throw new NotSupportedException();
		}

		public bool IsSorted
		{
			get
			{
				return false;
			}
		}

		public void RemoveIndex(PropertyDescriptor property)
		{
			// do nothing
		}

		public void RemoveSort()
		{
			throw new NotSupportedException();
		}

		public ListSortDirection SortDirection
		{
			get
			{
				throw new NotSupportedException();
			}
		}

		public PropertyDescriptor SortProperty
		{
			get
			{
				throw new NotSupportedException();
			}
		}

		public bool SupportsChangeNotification
		{
			get
			{
				return true;
			}
		}

		public bool SupportsSearching
		{
			get
			{
				return false;
			}
		}

		public bool SupportsSorting
		{
			get
			{
				return false;
			}
		}
		#endregion

		#region ICancelAddNew Members
		public void CancelNew(int itemIndex)
		{
			base.RemoveItem(itemIndex);
		}

		public void EndNew(int itemIndex)
		{
			// do nothing
		}
		#endregion
	}
}
